using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;

namespace GUI
{
    public partial class frmProfilesEdit : Form
    {
        ChameleonManager insChameleonManager = new ChameleonManager();
        public frmProfilesEdit()
        {
            InitializeComponent();
        }
        public frmProfilesEdit(Profile parProfile)
        {
            InitializeComponent();
            txtProfileName.ReadOnly = true;
            SetControls(parProfile);

        }
        public void SetControls(Profile parProfile)
        {
            txtProfileName.Text = parProfile.ProfileName;
            itbIpAddress.Text = parProfile.IpAddress;
            itbSubnetMask.Text = parProfile.SubnetMask;
            itbDefaultGateway.Text = parProfile.DefaultGateway;
            itbPreferredDnsServer.Text = parProfile.PreferredDns;
            itbAlternateDnsServer.Text = parProfile.AlternateDns;
            chkEnableProxy.Checked = parProfile.ProxyEnabled;
            txtProxyAddress.Text = parProfile.ProxyAddress;
            txtProxyPort.Text = parProfile.ProxyPort;
            chkByPassProxyForLocal.Checked = parProfile.ByPassProxyForLocal;
        }
        public Profile GetProfile()
        {
            Profile insProfile = new Profile();
            insProfile.ProfileName = txtProfileName.Text;
            insProfile.IpAddress = itbIpAddress.Text;
            insProfile.SubnetMask = itbSubnetMask.Text;
            insProfile.DefaultGateway = itbDefaultGateway.Text;
            insProfile.PreferredDns = itbPreferredDnsServer.Text;
            insProfile.AlternateDns = itbAlternateDnsServer.Text;
            insProfile.ByPassProxyForLocal = chkByPassProxyForLocal.Checked;
            insProfile.ProxyAddress = txtProxyAddress.Text;
            insProfile.ProxyEnabled = chkEnableProxy.Checked;
            insProfile.ProxyPort = txtProxyPort.Text;
            return insProfile;
        }
        public string ValidateProfile()
        {
            string errorMessage = "";
            if (txtProfileName.Text.Trim() == "")
            {
                errorMessage += "Profile Name can not be left blank\r\n";
            }
            if (itbIpAddress.Text.Trim() == "")
            {
                errorMessage += "Ip Address can not be left blank\r\n";
            }
            if (itbSubnetMask.Text.Trim() == "")
            {
                errorMessage += "Subnet Mask can not be left blank\r\n";
            }
            if (chkEnableProxy.Checked)
            {
                if (txtProxyAddress.Text.Trim() == "")
                {
                    errorMessage += "Proxy Address can not be left blank\r\n";
                }
                if (txtProxyPort.Text.Trim() == "")
                {
                    errorMessage += "Proxy Port can not be left blank\r\n";
                }
            }
            return errorMessage;
        }
        public void Save()
        {
            string errorMessage = ValidateProfile();
            if (errorMessage != "")
            {
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            errorMessage = insChameleonManager.SaveProfile(GetProfile(), txtProfileName.ReadOnly);
            if (errorMessage != "")
            {
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();

        }

        private void chkEnableProxy_CheckedChanged(object sender, EventArgs e)
        {
            gbpProxy.Enabled = chkEnableProxy.Checked;
        }

        private void txtProxyPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}